package pers.qianyu.month_202012.date_20201219.difference;

/**
 * 差分数组类封装
 *
 * @author mizzle rain
 * @date 2020-12-19 16:20
 */
public class Difference {
    private int[] diff;

    public Difference(int[] nums) {
        int len = nums.length;
        diff = new int[len];
        diff[0] = nums[0];
        for (int i = 1; i < nums.length; i++) {
            diff[i] = nums[i] - nums[i - 1];
        }
    }

    public void increment(int from, int to, int val) {
        diff[from] += val;
        if (to + 1 < diff.length) {
            diff[to + 1] -= val;
        }
    }

    public int[] result() {
        int[] res = new int[diff.length];
        res[0] = diff[0];
        for (int i = 1; i < diff.length; i++) {
            res[i] = res[i - 1] + diff[i];
        }
        return res;
    }
}
